package global_village.app;

import com.global_village.app.R;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener{

	private EditTextPreference serverIp;
	private SharedPreferences prefs;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.settings);
		prefs = PreferenceManager.getDefaultSharedPreferences(AppState.getCtx());
		InitGui();
	}

	private void InitGui(){
		//TODO - move to /@strings.
		serverIp = (EditTextPreference) getPreferenceScreen().findPreference("serverIp");
		
		serverIp.setSummary(prefs.getString("serverIp", "Enter server IP"));
	}

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if(key.equals("serverIp"))
			serverIp.setSummary(sharedPreferences.getString(key, ""));
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
}
